package com.dfbz.entity;

import com.dfbz.dao.MeetingDao;
import com.dfbz.utils.PageUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class MeetingService {
    private MeetingDao meetingDao = new MeetingDao();

    public PageUtils<Meeting> findMeetingList(Integer cureePage, long status, String title) {
        PageUtils<Meeting> meetingPageUtils = new PageUtils<>();
        Integer pageSize = meetingPageUtils.getPageSize();
        Integer totalCount = meetingDao.findMeetCount(title,status);
        Integer pageCount = totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount / pageSize)+1;
        List<Meeting> meetingList = meetingDao.findAll(status,title,(cureePage - 1) * pageSize,pageSize);
        meetingPageUtils.setPageCount(pageCount);
        meetingPageUtils.setCureePage(cureePage);
        meetingPageUtils.setObjList(meetingList);
        return meetingPageUtils;
    }

    public List<Map<String, Object>> findMeetingById(Long id) {
        return meetingDao.findMeetingById(id);
    }

    public String[] findMakeUserArr(Long id) {
        String makeUser = meetingDao.findMeetingMakeUserById(id);
        String makeUserWipe = makeUser.substring(1, makeUser.length() - 1);
        String[] makeUserArr = makeUserWipe.split(",");
        return makeUserArr;
    }

    public Long findReaComCount(Long id) {
       return meetingDao.findReaComCount(id);
    }

    public Integer isCome(Long uid, Long cid) {
        return meetingDao.isCome(uid,cid);
    }

    public boolean canCome(Long uid, Long cid) {
        String[] makeUserArr = findMakeUserArr(cid);
        return Arrays.asList(makeUserArr).contains(uid.toString());
    }

    public void comeMeeting(Long uid, Long cid) {
        meetingDao.comeMeeting(uid,cid);
    }

    public void unComeMeeting(Long uid, Long cid) {
        meetingDao.unComeMeeting(uid,cid);
    }

    public void addMeeting(Meeting meeting) {
        meeting.setPublishDate(new Date());
        meeting.setStatus(0L);
        meetingDao.addMeeting(meeting);
    }
}
